/*
 * Copyright 2017 Baiyyy
 * @(#)DeviceServiceImpl.java
 */

package com.byyy.ccts.modules.cc.service.impl;

import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.byyy.ccts.core.common.service.impl.CommonServiceImpl;
import com.byyy.ccts.core.query.data.PageImpl;
import com.byyy.ccts.core.query.data.Pageable;
import com.byyy.ccts.core.query.data.Queryable;
import com.byyy.ccts.core.query.parse.QueryToWrapper;
import com.byyy.ccts.modules.cc.entity.ccts.TADevice;
import com.byyy.ccts.modules.cc.mapper.DeviceMapper;
import com.byyy.ccts.modules.cc.service.IDeviceService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by DELL on 2018-5-14.
 */
@Transactional
@Service("deviceService")
public class DeviceServiceImpl extends CommonServiceImpl<DeviceMapper, TADevice> implements IDeviceService {

    /**
     * 装车单明细内查询的设备列表
     * @param queryable
     * @param wrapper
     * @return
     */
    public com.byyy.ccts.core.query.data.Page<TADevice> deviceListForZcdMx(Queryable queryable, Wrapper<TADevice> wrapper, List<String> zhuangcdNos) {
        QueryToWrapper<TADevice> queryToWrapper = new QueryToWrapper<>();
        queryToWrapper.parseCondition(wrapper, queryable);
        // 排序问题
        queryToWrapper.parseSort(wrapper, queryable);
        Pageable pageable = queryable.getPageable();
        com.baomidou.mybatisplus.plugins.Page<TADevice> page = new com.baomidou.mybatisplus.plugins.Page<TADevice>(
                pageable.getPageNumber(), pageable.getPageSize());
        com.baomidou.mybatisplus.plugins.Page<TADevice> content = deviceListPageForZcdMx(page, wrapper, zhuangcdNos);
        return new PageImpl<TADevice>(content.getRecords(), queryable.getPageable(), content.getTotal());
    }

    /**
     * 装车单明细内查询的设备列表
     * @param page
     * @param wrapper
     * @return
     */
    public Page<TADevice> deviceListPageForZcdMx(Page<TADevice> page, Wrapper<TADevice> wrapper, List<String> zhuangcdNos) {
        page.setRecords(baseMapper.selectDevicePage(page, wrapper, zhuangcdNos));
        return page;
    }

    /**
     * 装车单明细内查询的设备列表
     * @param queryable
     * @param wrapper
     * @return
     */
    public com.byyy.ccts.core.query.data.Page<TADevice> deviceInfoList(Queryable queryable, Wrapper<TADevice> wrapper) {
        QueryToWrapper<TADevice> queryToWrapper = new QueryToWrapper<>();
        queryToWrapper.parseCondition(wrapper, queryable);
        // 排序问题
        queryToWrapper.parseSort(wrapper, queryable);
        Pageable pageable = queryable.getPageable();
        com.baomidou.mybatisplus.plugins.Page<TADevice> page = new com.baomidou.mybatisplus.plugins.Page<TADevice>(
                pageable.getPageNumber(), pageable.getPageSize());
        com.baomidou.mybatisplus.plugins.Page<TADevice> content = deviceInfoListPage(page, wrapper);
        return new PageImpl<TADevice>(content.getRecords(), queryable.getPageable(), content.getTotal());
    }

    /**
     * 装车单明细内查询的设备列表
     * @param page
     * @param wrapper
     * @return
     */
    public Page<TADevice> deviceInfoListPage(Page<TADevice> page, Wrapper<TADevice> wrapper) {
        page.setRecords(baseMapper.selectDeviceInfoPage(page, wrapper));
        return page;
    }

    /**
     * 查询设备信息
     * */
    @Override
    public List<TADevice> selectDeviceInfo(List<String> ids) {
        return this.baseMapper.selectDeviceInfo(ids);
    }

    /**
     * 微信小程序接口查看设备最近的5个温度值
     * */
    @Override
    public List<TADevice> queryTheLatestTemp(String id) {
        return this.baseMapper.queryTheLatestTemp(id);
    }

    /**
     * 根据设备SN查询设备信息
     * @param sn
     * @return
     */
    @Override
    public TADevice selectBySn(String sn) {
        return this.baseMapper.selectBySn(sn);
    }

    /**
     * 根据设备SN查询设备信息
     * @param nos
     * @return
     */
    @Override
    public List<TADevice> queryByNoAndCangku(List<String> nos, String cangku) {
        return this.baseMapper.queryByNoAndCangku(nos, cangku);
    }

}
